package com.example.f22015232.controller;

import com.example.f22015232.model.Student;
import com.example.f22015232.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


import java.util.List;

@Controller
@RequestMapping("student")
public class StudentController {

    private final StudentService studentService;

    @Autowired
    public StudentController(StudentService studentService) {
        this.studentService = studentService;
    }

    /**
     * 显示当前用户的学生列表
     */
    @RequestMapping("list")
    public String list(ModelMap modelMap) {
        // 获取当前登录用户
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();

        // 根据用户名加载学生数据
        List<Student> studentList = studentService.getStudentsForUser(username);
        modelMap.put("studentList", studentList);

        return "student/list"; // 返回学生列表页面
    }

    /**
     * 根据条件搜索学生
     */
    @RequestMapping("search")
    public String search(Student student, ModelMap modelMap) {
        // 获取当前登录用户
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();

        // 根据学生信息和当前用户进行搜索
        List<Student> studentList = studentService.search(student, username);
        modelMap.put("studentList", studentList);

        return "student/list"; // 返回学生列表页面
    }

    /**
     * 保存新学生
     */
    @RequestMapping("save")
    public String save(Student student) {
        try {
            // 获取当前登录用户
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            String username = authentication.getName();

            // 设置创建者
            student.setCreatedBy(username);
            studentService.save(student);
        } catch (Exception e) {
            e.printStackTrace(); // 打印错误日志
            return "error"; // 跳转到错误页面
        }
        return "redirect:/student/list"; // 保存成功后重定向到列表页面
    }

    /**
     * 编辑学生信息
     */
    @RequestMapping("edit/{id}")
    public String edit(@PathVariable Integer id, ModelMap map) {
        Student student = studentService.get(id);
        map.put("student", student); // 将学生数据传递到模板
        return "student/edit"; // 返回 edit.html 页面
    }


    /**
     * 更新学生信息
     */
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public String update(Student student) {
        try {
            // 获取当前登录用户
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            String username = authentication.getName();

            // 设置 createdBy 字段
            student.setCreatedBy(username);

            // 调用服务层更新方法
            studentService.update(student);
        } catch (Exception e) {
            e.printStackTrace(); // 打印错误日志
            return "error"; // 返回错误页面
        }
        return "redirect:/student/list"; // 更新成功后重定向到列表页面
    }




    /**
     * 根据 ID 删除学生
     */
    @RequestMapping("delete/{id}")
    public String delete(@PathVariable Integer id) {
        studentService.delete(id);
        return "redirect:/student/list"; // 删除成功后重定向到列表页面
    }
}
